/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { PlusOutlined } from '@ant-design/icons';
import type { RcFile } from 'rc-upload/lib/interface';
import { message } from 'antd';

export const checkUpFiles = (file: RcFile, checkType: string, checkSize: number = 50) => {
  const { size, type, name } = file;

  if (checkType == 'checkImg') {
    const fileType = type.split('/')[1];
    const isLt5M = size / 1024 / 1024 < 5;
    const hasImgAccept = ['jpeg', 'png', 'jpg'].includes(fileType);
    if (!isLt5M) {
      message.error('图片尺寸大于5M');
      return false;
    } else if (!hasImgAccept) {
      message.error('仅支持png、jpg格式，请重新选择');
      return false;
    } else {
      return true;
    }
  } else if (checkType == 'checkFile') {
    if (size > checkSize * 1000) {
      message.error(
        `上传失败，上传文件${name}时出错：上传文件不能超过50KB，当前文件${size / 1000}KB`,
      );
      return false;
    } else {
      return true;
    }
  } else {
    return true;
  }
};

export const getFileUrl = (data: RcFile[]) => data.map((item: any) => item.url);

export const uploadButton = (
  <div style={{ color: '#4ca4f8' }}>
    <PlusOutlined />
    <div style={{ marginTop: 8 }}>点击上传素材</div>
  </div>
);
